home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / IDL.PY < prev    next >
Encoding:
Text File  |  1999-07-28  |  12.4 KB  |  447 lines

  1.  
  2. # idl grammar
  3. #
  4. # Note, this grammar requires a special hack at the lexical
  5. # level in order to parse the fragment
  6. #
  7. # ...
  8. #  case abc::def: jjj::www: whatever...
  9. #
  10. # (Yuck!)
  11. # Some would argue this is a language design flaw, but whatever...
  12. # It causes a shift/reduce problem without special handling for ::
  13. # below coloncolon is a 'fake' keyword that parses as two colons.
  14.  
  15. idlgramstring = """
  16.  
  17. specification ::
  18.  
  19. ## 1
  20. @R r1a :: specification >> definition speclist
  21. @R r1b :: speclist >> specification
  22. @R r1c :: speclist >>
  23.  
  24. ## 2 punct ;
  25. @R r2a :: definition >> type_dcl ;
  26. @R r2b :: definition >> const_dcl ;
  27. @R r2c :: definition >> except_dcl ;
  28. @R r2d :: definition >> interface_nt ;
  29. @R r2e :: definition >> module_nt ;
  30.  
  31. ## 3 identifier=term, module=kw puncts {}
  32. @R r3 :: module_nt >> module identifier { specification }
  33.  
  34. ## 4
  35. @R r4a :: interface_nt >> interface_dcl
  36. @R r4b :: interface_nt >> forward_dcl
  37.  
  38. ## 5
  39. @R r5 :: interface_dcl >> interface_header { interface_body }
  40.  
  41. ## 6 interface=kw
  42. @R r6 :: forward_dcl >> interface identifier
  43.  
  44. ## 7 puncts []
  45. @R r7 :: interface_header >> interface identifier [ inheritance_spec ]
  46.  
  47. ## 8 
  48. @R r8a :: interface_body >> 
  49. @R r8b :: interface_body >> export interface_body
  50.  
  51. ## 9
  52. @R r9a :: export >> type_dcl
  53. @R r9b :: export >> const_dcl
  54. @R r9c :: export >> except_dcl
  55. @R r9d :: export >> attr_dcl
  56. @R r9e :: export >> op_dcl
  57.  
  58. ## 10 punct ,:
  59. @R r10a :: inheritance_spec >> : scoped_name_list
  60. @R r10b :: scoped_name_list >> scoped_name
  61. @R r10c :: scoped_name_list >> scoped_name_list , scoped_name
  62.  
  63. ## 11
  64. @R r11a :: scoped_name >> identifier
  65. @R r11b :: scoped_name >> colon_colon identifier
  66. @R r11d :: scoped_name >> scoped_name coloncolon identifier
  67.  
  68. ## 12 const=kw punct =
  69. @R r12 :: const_dcl >> const const_type identifier = const_expr
  70.  
  71. ## 13
  72. @R r13a :: const_type >> integer_type
  73. @R r13b :: const_type >> char_type
  74. @R r13c :: const_type >> boolean_type
  75. @R r13d :: const_type >> floating_type
  76. @R r13e :: const_type >> string_type
  77. @R r13f :: const_type >> scoped_name
  78.  
  79. ## 14
  80. @R r14 :: const_expr >> or_expr
  81.  
  82. ##15 punct |
  83. @R r15a :: or_expr >> xor_expr
  84. @R r15b :: or_expr >> or_expr | xor_expr
  85.  
  86. ##16 punct ^
  87. @R r16a :: xor_expr >> and_expr
  88. @R r16b :: xor_expr >> xor_expr ^ and_expr
  89.  
  90. ##17 punct &
  91. @R r17a :: and_expr >> shift_expr
  92. @R r17b :: and_expr >> and_expr & shift_expr
  93.  
  94. ##18 punct > <
  95. @R r18a :: shift_expr >> add_expr
  96. @R r18b :: shift_expr >> shift_expr > > add_expr
  97. @R r18c :: shift_expr >> shift_expr < < add_expr
  98.  
  99. ##19 punct +-
  100. @R r19a :: add_expr >> mult_expr
  101. @R r19b :: add_expr >> add_expr + mult_expr
  102. @R r19c :: add_expr >> add_expr - mult_expr
  103.  
  104. ##20 punct */%
  105. @R r20a :: mult_expr >> unary_expr
  106. @R r20b :: mult_expr >> mult_expr * unary_expr
  107. @R r20c :: mult_expr >> mult_expr / unary_expr
  108. @R r20d :: mult_expr >> mult_expr % unary_expr
  109.  
  110. ##21
  111. @R r21a :: unary_expr >> unary_operator primary_expr
  112. @R r21b :: unary_expr >> primary_expr
  113.  
  114. ##22
  115. @R r22a :: unary_operator >> -
  116. @R r22b :: unary_operator >> +
  117. @R r22c :: unary_operator >> ~
  118.  
  119. ##23 punct ()
  120. @R r23a :: primary_expr >> scoped_name
  121. @R r23b :: primary_expr >> literal
  122. @R r23c :: primary_expr >> ( const_expr )
  123.  
  124. ##24 terms = *_literal (?) except boolean
  125. @R r24a :: literal >> integer_literal
  126. @R r24b :: literal >> string_literal
  127. @R r24c :: literal >> character_literal
  128. @R r24d :: literal >> floating_pt_literal
  129. @R r24e :: literal >> boolean_literal
  130.  
  131. ##25 kw TRUE FALSE
  132. @R r25a :: boolean_literal >> TRUE
  133. @R r25b :: boolean_literal >> FALSE
  134.  
  135. ##26 
  136. @R r26 :: positive_int_literal >> const_expr
  137.  
  138. ##27 kw typedef
  139. @R r27a :: type_dcl >> typedef type_declarator
  140. @R r27b :: type_dcl >> struct_type
  141. @R r27c :: type_dcl >> union_type
  142. @R r27d :: type_dcl >> enum_type
  143.  
  144. ##28
  145. @R r28 :: type_declarator >> type_spec declarators
  146.  
  147. ##29
  148. @R r29a :: type_spec >> simple_type_spec
  149. @R r29b :: type_spec >> constr_type_spec
  150.  
  151. ##30
  152. @R r30a :: simple_type_spec >> base_type_spec
  153. @R r30b :: simple_type_spec >> template_type_spec
  154. @R r30c :: simple_type_spec >> scoped_name
  155.  
  156. ##31
  157. @R r31a :: base_type_spec >> floating_pt_type
  158. @R r31b :: base_type_spec >> integer_type
  159. @R r31c :: base_type_spec >> char_type
  160. @R r31d :: base_type_spec >> boolean_type
  161. @R r31e :: base_type_spec >> octet_type
  162. @R r31f :: base_type_spec >> any_type
  163.  
  164. ## 32
  165. @R r32a :: template_type_spec >> sequence_type
  166. @R r32b :: template_type_spec >> string_type
  167.  
  168. ##33
  169. @R r33a :: constr_type_spec >> struct_type
  170. @R r33b :: constr_type_spec >> union_type
  171. @R r33c :: constr_type_spec >> enum_type
  172.  
  173. ##34
  174. @R r34a :: declarators >> declarator
  175. @R r34b :: declarators >> declarators , declarator
  176.  
  177. ##35
  178. @R r35a :: declarator >> simple_declarator
  179. @R r35b :: declarator >> complex_declarator
  180.  
  181. ##36
  182. @R r36 :: simple_declarator >> identifier
  183.  
  184. ##37
  185. @R r37 :: complex_declarator >> array_declarator
  186.  
  187. ##38 kw float double
  188. @R r38a :: floating_pt_type >> float
  189. @R r38b :: floating_pt_type >> double
  190.  
  191. ##39 
  192. @R r39a :: integer_type >> signed_int
  193. @R r39b :: integer_type >> unsigned_int
  194.  
  195. ##40
  196. @R r40 :: signed_int >> signed_long_int
  197. @R r40 :: signed_int >> signed_short_int
  198.  
  199. ##41 kw long
  200. @R r41 :: signed_long_int >> long
  201.  
  202. ##42 kw short
  203. @R r42 :: signed_short_int >> short
  204.  
  205. ##43
  206. @R r43 :: unsigned_int >> unsigned_long_int
  207. @R r43 :: unsigned_int >> unsigned_short_int
  208.  
  209. ##44 kw unsigned
  210. @R r44 :: unsigned_long_int >> unsigned long
  211.  
  212. ##45 
  213. @R r45 :: unsigned_short_int >> unsigned short
  214.  
  215. ##46 kw char
  216. @R r46 :: char_type >> char
  217.  
  218. ##47 kw boolean
  219. @R r47 :: boolean_type >> boolean
  220.  
  221. ##48 kw octet
  222. @R r48 :: octet_type >> octet
  223.  
  224. ##49 kw any
  225. @R r49 :: any_type >> any
  226.  
  227. ##50 kw struct
  228. @R r50 :: struct_type >> struct identifier { member_list }
  229.  
  230. ##51
  231. @R r51a :: member_list >> member
  232. @R r51b :: member_list >> member_list member
  233.  
  234. ##52
  235. @R r52 :: member >> type_spec declarators ;
  236.  
  237. ##53 kw union switch
  238. @R r53 :: union_type >> 
  239.     union identifier switch ( switch_type_spec ) { switch_body }
  240.  
  241. ##54
  242. @R r54a :: switch_type_spec >> integer_type
  243. @R r54b :: switch_type_spec >> char_type
  244. @R r54c :: switch_type_spec >> boolean_type
  245. @R r54d :: switch_type_spec >> enum_type
  246. @R r54e :: switch_type_spec >> scoped_name
  247.  
  248. ##55
  249. @R r55a :: switch_body >> case_nt
  250. @R r55b :: switch_body >> switch_body case_nt
  251.  
  252. ##56
  253. @R r56a :: case_nt >> case_labels element_spec ;
  254. @R r56b :: case_labels >> case_label
  255. @R r56c :: case_labels >> case_labels case_label
  256.  
  257.  
  258. ##57 kw default case
  259. @R r57a :: case_label >> case const_expr : 
  260. @R r57b :: case_label >> default :
  261.  
  262. ##58
  263. @R r58 :: element_spec >> type_spec declarator
  264.  
  265. ##59 kw enum
  266. @R r59a :: enum_type >> enum identifier { enumerators }
  267. @R r59b :: enumerators >> enumerator
  268. @R r59c :: enumerators >> enumerators , enumerator
  269.  
  270. ##60
  271. @R r60 :: enumerator >> identifier
  272.  
  273. ##61 kw sequence
  274. @R r61 :: sequence_type >> sequence < simple_type_spec , positive_int_const >
  275.  
  276. ##62 kw string
  277. @R r62a :: string_type >> string < positive_int_const >
  278. @R r62b :: string_type >> string
  279.  
  280. ##63
  281. @R r63a :: array_declarator >> identifier fixed_array_sizes
  282. @R r63b :: fixed_array_sizes >> fixed_array_size
  283. @R r63c :: fixed_array_sizes >> fixed_array_sizes fixed_array_size
  284.  
  285. ##64
  286. @R r64 :: fixed_array_size >> [ positive_int_const ]
  287.  
  288. ##65 kw attribute readonly
  289. @R r65a :: attr_dcl >> maybe_readonly attribute param_type_spec simple_declarators
  290. @R r65b :: maybe_readonly >> readonly
  291. @R r65c :: maybe_readonly >>
  292. @R r65d :: simple_declarators >> simple_declarator
  293. @R r65e :: simple_declarators >> simple_declarators , simple_declarator
  294.  
  295. ##66 kw exception
  296. @R r66a :: except_dcl >> exception identifier { members }
  297. @R r66b :: members >>
  298. @R r66c :: members >> member_list
  299.  
  300. ##67
  301. @R r67a :: op_dcl >> 
  302.    maybe_op_attribute op_type_spec identifier parameter_dcls
  303.    maybe_raises_expr maybe_context_expr
  304. @R r67b :: maybe_op_attribute >> 
  305. @R r67c :: maybe_op_attribute >> op_attribute
  306. @R r67d :: maybe_raises_expr >>
  307. @R r67e :: maybe_raises_expr >> raises_expr
  308. @R r67f :: maybe_context_expr >>
  309. @R r67g :: maybe_context_expr >> context_expr
  310.  
  311. ##68 kw oneway
  312. @R r68a :: op_attribute >> oneway
  313.  
  314. ##69 kw void
  315. @R r69a :: op_type_spec >> param_type_spec
  316. @R r69b :: op_type_spec >> void
  317.  
  318. ##70
  319. @R r70a :: parameter_dcls >> ( parameterlist )
  320. @R r70b :: parameter_dcls >> (  )
  321. @R r70c :: parameterlist >> param_dcl
  322. @R r70d :: parameterlist >> parameterlist , param_dcl
  323.  
  324. ##71
  325. @R r71 :: param_dcl >> param_attribute param_type_spec simple_declarator
  326.  
  327. ##72 kw in out inout
  328. @R r72 :: param_attribute >> in
  329. @R r72 :: param_attribute >> out
  330. @R r72 :: param_attribute >> inout
  331.  
  332. ##73 kw raises
  333. @R r73 :: raises_expr >> raises ( scoped_name_list )
  334.  
  335. ##74 kw context
  336. @R r74 :: context_expr >> context ( string_literal_list )
  337. @R r74b :: string_literal_list >> string_literal
  338. @R r74c :: string_literal_list >> string_literal_list , string_literal
  339.  
  340. @R r75 :: param_type_spec >> base_type_spec
  341. @R r75 :: param_type_spec >> string_type
  342. @R r75 :: param_type_spec >> scoped_name
  343.  
  344. """
  345.  
  346. nonterms = """
  347. colon_colon
  348. param_attribute
  349. unsigned_long_int unsigned_short_int param_dcl
  350. parameterlist string_literal_list
  351. members maybe_op_attribute maybe_raises_expr maybe_context_expr
  352. op_type_spec parameter_dcls op_attribute raises_expr context_expr
  353. maybe_readonly param_type_spec simple_declarators simple_declarator
  354. fixed_array_sizes fixed_array_size
  355. element_spec enumerator enumerators
  356. switch_type_spec switch_body case_nt case_labels case_label
  357. member_list member
  358. signed_int unsigned_int signed_long_int signed_short_int
  359. simple_declarator complex_declarator array_declarator
  360. declarator 
  361. sequence_type string_type
  362. floating_pt_type integer_type char_type boolean_type
  363. octet_type any_type
  364. base_type_spec template_type_spec
  365. simple_type_spec constr_type_spec
  366. type_spec declarators
  367. type_declarator struct_type union_type enum_type
  368. literal boolean_literal positive_int_literal
  369. mult_expr unary_expr unary_operator primary_expr
  370. or_expr xor_expr and_expr shift_expr add_expr
  371. integer_type char_type boolean_type floating_type string_type
  372. const_type const_expr
  373. scoped_name_list scoped_name
  374. attr_dcl op_dcl
  375. inheritance_spec export
  376. interface_header interface_body
  377. interface_dcl forward_dcl
  378. type_dcl const_dcl except_dcl interface_nt module_nt
  379. specification definition speclist
  380. """
  381.  
  382. keywords = """
  383. exception oneway void in out inout raises context
  384. interface module const TRUE FALSE typedef float double long
  385. unsigned short char boolean octet any struct union switch
  386. enum string attribute readonly default case sequence ::
  387. """ 
  388. # NOTE: FOR NECESSARY HACKERY REASONS :: IS A KEYWORD!
  389.  
  390. punctuations = ";{}()[],:|^&<>+-*/%~="
  391.  
  392. # dummy regexen
  393. identifierre = "identifier"
  394. integer_literalre = "123"
  395. positive_int_constre = "999"
  396. string_literalre = "'string'"
  397. character_literalre= "'c'"
  398. floating_pt_literalre = "1.23"
  399.  
  400.  
  401. # dummy interp fun for all terminals
  402. def echo (str):
  403.     return str
  404.  
  405. def DeclareTerminals(Grammar):
  406.     Grammar.Addterm("identifier", identifierre, echo)
  407.     Grammar.Addterm("integer_literal", integer_literalre, echo)
  408.     Grammar.Addterm("string_literal", string_literalre, echo)
  409.     Grammar.Addterm("character_literal", character_literalre, echo)
  410.     Grammar.Addterm("floating_pt_literal", floating_pt_literalre, echo)
  411.     Grammar.Addterm("positive_int_const", positive_int_constre, echo)
  412.  
  413. ## we need to override LexDictionary to recognize :: as a SINGLE punctuation.
  414. ## (not possible using standard kjParsing, requires a special override)
  415. import kjParser
  416. class myLexDictionary(kjParser.LexDictionary):
  417.    def __init__(self):
  418.        kjParser.LexDictionary.__init__(self)
  419.        map = ((kjParser.KEYFLAG, "coloncolon"), "coloncolon")
  420.        self.keywordmap["::"] = map
  421.        self.keywordmap["coloncolon"] = map
  422.        
  423.    def Token(self, String, StartPosition):
  424.        if String[StartPosition:StartPosition+2] == "::":
  425.           tok = self.keywordmap["::"]
  426.           return (tok, 2)
  427.        # default:
  428.        return kjParseBuild.LexDictionary.Token(self, String, StartPosition)
  429.  
  430. # default bind all rules
  431.  
  432. def GrammarBuild():
  433.     import kjParseBuild
  434.     idl = kjParseBuild.NullCGrammar()
  435.     idl.LexD = myLexDictionary()
  436.     #idl.SetCaseSensitivity(0) # grammar is not case sensitive for keywords
  437.     DeclareTerminals(idl)
  438.     idl.Keywords(keywords)
  439.     idl.punct(punctuations)
  440.     idl.Nonterms(nonterms)
  441.     #idl.comments([LISPCOMMENTREGEX])
  442.     idl.Declarerules(idlgramstring)
  443.     print "now compiling"
  444.     idl.Compile()
  445.     return idl
  446.  
  447. if __name__=="__main__": GrammarBuild()